﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;
using UDS.Entity;

namespace UDS.SubModule.Position
{
    /// <summary>
    /// ListView 的摘要说明。
    /// </summary>
    public partial class ListViewPage : Page
    {
        protected bool ShowNormalStaffs
        {
            get;
            private set;
        }

        protected int PositionID
        {
            get;
            private set;
        }

        protected override void OnInit(EventArgs e)
        {
            // Read query string parameters
            int value = QSUtil.GetPositiveInt32("DisplayType", 0);
            ShowNormalStaffs = (value == 0);
            cmdOnPosition.Visible = ShowNormalStaffs;
            cmdOffPosition.Visible = !ShowNormalStaffs;

            PositionID = QSUtil.GetPositiveInt32("Position_ID", 0);
            if (PositionID <= 0)
            {
                PositionID = QSUtil.GetPositiveInt32("PositionID", 0);
            }
            if (PositionID <= 0)
            {
                PositionID = 1;
            }

            // Create event handlers
            cmdOnPosition.Click += cmdOnPosition_Click;
            cmdOffPosition.Click += cmdOffPosition_Click;
            cmdChangePosition.Click += cmdChangePosition_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //如果要求刷新部门树
                if (Request.QueryString["Refresh"] != null)
                {
                    Response.Write("<script language='javascript'>parent.LeftFrame.location.reload();</script>");
                }

                cmdOnPosition.Attributes.Add("Onclick", "javascript:return confirm('是否让选中的人复职？');");
                cmdOffPosition.Attributes.Add("Onclick", "javascript:return confirm('是否让选中的人离职？');");

                BindGrid();
            }

            base.OnLoad(e);
        }

        private void BindGrid()
        {
            dbStaffList.DataSource = Staff.GetStaffInPosition(PositionID, false);
            dbStaffList.DataBind();
        }

        //离职,复职通知
        private void sms_all(int i)
        {
            IList<int> list = Tools.FindSelectedItems(dbStaffList, "Staff_ID");
            var selectedStaffs = Staff.GetStaffInfo(list);
            if (selectedStaffs.Count > 0)
            {
                var staffList = Staff.GetAllStaff(false);
                foreach (var selectedStaff in selectedStaffs)
                {
                    var sm = new SMS();

                    string currentUser = SiteContext.Current.UserName;
                    //处理短信提醒

                    string Position_name = selectedStaff.PositionName;
                    foreach (var staff in staffList)
                    {
                        string msg;
                        if (i == 0)
                        {
                            msg = Position_name + " 处员工:" + selectedStaff.RealName + ",已经离职,特此通知.";
                        }
                        else
                        {
                            msg = Position_name + " 处员工:" + selectedStaff.RealName + ",已经恢复原职,特此通知.";
                        }
                        string Staff_name = staff.Name;
                        SMS.SendLocalMsg(currentUser, new string[] { Staff_name }, msg);
                    }
                }
            }
        }

        private void cmdOnPosition_Click(object sender, EventArgs e)
        {
            IList<int> list = Tools.FindSelectedItems(dbStaffList, "Staff_ID");
            if (list.Count > 0)
            {
                Staff.Rehab(list);
                if (cbRemind.Checked)
                {
                    sms_all(1);
                }
            }
            Response.Redirect("Listview.aspx?PositionID=" + PositionID + "&displayType=" + (ShowNormalStaffs ? 0 : 1));
        }

        private void cmdOffPosition_Click(object sender, EventArgs e)
        {
            IList<int> list = Tools.FindSelectedItems(dbStaffList, "Staff_ID");
            if (list.Count > 0)
            {
                if (Staff.Dimission(list))
                {
                    if (cbRemind.Checked)
                    {
                        sms_all(0);
                    }
                    //Response.Write("<script language=javascript>alert('离职成功！');</script>");		
                    BindGrid();
                }
            }
            else
            {
                Response.Write("<script language=javascript>alert('请选择要离职的人员！');</script>");
            }
        }

        private void cmdChangePosition_Click(object sender, EventArgs e)
        {
            IList<int> list = Tools.FindSelectedItems(dbStaffList, "Staff_ID");
            if (list.Count > 0)
            {
                var intArray = new int[list.Count];
                list.CopyTo(intArray, 0);

                string[] stringArray = Array.ConvertAll(intArray, i => i.ToString());
                string selectedID = string.Join(",", stringArray);
                Response.Redirect("ChangePosition.aspx?PositionID=" + PositionID + "&StaffIDS=" + selectedID + "&DisplayType=" + (ShowNormalStaffs ? 0 : 1) + "&BackFilePath=" + Request.CurrentExecutionFilePath);
            }
            else
            {
                Response.Write("<script language=javascript>alert('请选择要调职的人员！');</script>");
            }
        }

        #region 翻页事件

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dbStaffList.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        #endregion
    }
}